<template>
  <a-modal :open="open" width="1400px" title="新增检验申请单" centered @cancel="onCancel" @ok="onOk">
    <a-card class="patient-info-header" :body-style="{ padding: '8px 12px' }">
      <div class="flex justify-between items-center">
        <div class="flex items-center flex-1 overflow-hidden text-white">
          <div>
            <a-avatar :size="48" :src="avatarMan" />
          </div>
          <LabelValue class="text-lg ml-5" label="姓名" :width="150" :value="store.currentPatient?.brName" />
          <LabelValue label="性别" :width="100" :value="store.currentPatient?.brXb" />
          <LabelValue label="年龄" :width="100" :value="store.currentPatient?.brAge" />
          <LabelValue label="信息卡号" :width="150" :value="store.currentPatient?.brId" />
          <LabelValue class="mx-1" label="诊断" :width="200" :tooltip="store.currentPatient.ryzd" :value="store.currentPatient?.ryzd" />
          <LabelValue label="主治医生" :width="150" :value="store.currentPatient?.lastAttendingDoctorName" />
          <LabelValue label="科室" :width="150" :value="store.currentPatient?.ksmc" />
          <LabelValue label="病区" :width="150" :value="store.currentPatient?.wardName" />
          <LabelValue label="床位号" :width="150" :value="store.currentPatient?.lastBedCode" />
        </div>
      </div>
    </a-card>
    <div class="flex mt-7">
      <div class="w-[650px] mx-4">
        <BaseTable
          column-code="000228"
          :data-source="tableData"
          :custom-row="onClickRow"
          row-key="categroyCode"
          :row-class-name="(record) => (record.categroyCode === categroyCode ? 'selected-row cursor-pointer' : 'cursor-pointer')"
          :scroll="{ x: 400, y: 430 }"
          :height="380"
          :loading="loading"
        >
          <template #bodyCell="{ column, index }">
            <template v-if="column.dataIndex == 'xh'">
              {{ index + 1 }}
            </template>
          </template>
        </BaseTable>
      </div>
      <div class="w-[660px]">
        <BaseTable
          column-code="000229"
          :data-source="projectData"
          row-key="id"
          :scroll="{ x: 400, y: 430 }"
          :height="380"
          :loading="loadingProject"
          :row-selection="rowSelection"
        />
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import useAppStore from '@/store/user'
import avatarMan from '@/assets/images/avatar-man.png'
import useStore from '../../store'

const store = useStore()
const storeUser = useAppStore()
const categroyCode = ref('')
const categroyRow = ref({})
const selectedRowKeys = ref([])
const selectedRows = ref([])
const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const { loading, getData, tableData } = useTable({
  url: '/lis/InstrumentType/getLisCategroyList',
  formatResult: (data) => {
    categroyCode.value = props.rowData.categroyCode || data[0]?.categroyCode
    selectedRowKeys.value = []
    selectedRows.value = []
    getProjectData()
    return data
  }
})

// lsh: props.tableRow.lsh
const {
  loading: loadingProject,
  getData: getProjectData,
  tableData: projectData
} = useTable({
  url: '/lis/LisApply/getlisInstrumentTypeRelationByCategroyCodeList',
  formatParams: () => {
    return {
      categroyCode: categroyCode.value
    }
  },
  formatResult: (data) => {
    const arr = data.map((item) => {
      return {
        ...item,
        id: item.lisSetId || item.orderItemCode,
        type: item.lisSetId ? '组套' : '项目'
      }
    })
    // selectedRowKeys.value = arr.map((item) => item.id)
    // selectedRows.value = arr
    return arr
  }
})

const onClickRow = (record) => {
  return {
    onClick: () => {
      categroyCode.value = record.categroyCode
      categroyRow.value = record
      selectedRowKeys.value = []
      selectedRows.value = []
      getProjectData()
    }
  }
}
const rowSelection = ref({
  checkStrictly: true,
  selectedRowKeys: selectedRowKeys,
  onChange: (keys, rows) => {
    selectedRowKeys.value = keys
    selectedRows.value = rows
  }
})
const onOk = async () => {
  if (selectedRowKeys.value.length === 0) return message.error('请选择项目')

  const orderItemCodeList = []
  const lisSetIdList = []
  selectedRows.value.forEach((item) => {
    if (item.type === '组套') {
      lisSetIdList.push(item.id)
    } else {
      orderItemCodeList.push(item.id)
    }
  })
  const { code, msg } = await post('/lis/LisApply/doSaveLisApply', {
    apply: 1,
    ...props.rowData,
    ...categroyRow.value,
    lisSetIdList,
    orderItemCodeList,
    brId: store.currentPatient.brId,
    applyPath: 1,
    key: store.currentPatient.inplsh,
    applyDoctorId: storeUser.empId,
    applyDeptCode: store.currentPatient?.lastDeptCode,
    categroyCode: categroyCode.value,
    categroyName: categroyRow.value.categroyName,
    bcLczdZddm: store.currentPatient?.icd10Code,
    brName: store.currentPatient?.brName
  })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      getData()
    }
  }
)
</script>
<style lang="less" scoped>
.patient-info-header {
  background-color: var(--primary-color);
}
</style>
